#include "filehandler.h"
#include "taggedfile.h"
#include <QDir>
#include <QDebug>

FileHandler::FileHandler()
{
}

bool FileHandler::removeFile(QString fileName)
{
    bool ret = false;
    ret = QFile::remove(fileName);
    if(ret == false)
    {
        qWarning() << fileHandlerStr << fileHandlerRmvFileStr
                   << fileNotRemovedStr;
        return ret;
    }
    return ret;
}

bool FileHandler::isFileExist(QString fileName)
{
    bool ret= false;
    ret = QFile::exists(fileName);
    if(ret == false)
    {
        qDebug() << fileHandlerStr << fileHandlerIsFileExist
                 << fileNotExistStr;
        return ret;
    }
    return ret;
}

bool FileHandler::getMusic(QString folder, Collection & collection)
{
    QDir dir(folder);
    QStringList filters;
    filters << "*.mp3" << "*.ogg" << "*.flac";
    dir.setNameFilters(filters);
    if(dir.exists())
    {
        QStringList listFiles = dir.entryList(QDir::Readable | QDir::Files);

        for(QStringList::ConstIterator it = listFiles.begin();
            it != listFiles.constEnd(); it ++)
        {
            TaggedFile::file(dir.absolutePath() + "/" + (*it), collection);
        }
        dir.setNameFilters(QStringList());
        QStringList listDirs = dir.entryList(QDir::Readable | QDir::Dirs |
                                             QDir::NoDotAndDotDot);
        for(QStringList::ConstIterator it = listDirs.begin();
            it != listDirs.constEnd(); it ++)
        {
            getMusic(dir.absolutePath() + "/" + (*it), collection);
        }
        return true;
    }
    return false;
}
